package com.orangelife.common;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.orangelife.redis.core.redis.session.redisSession.RedisSessionFactory;
import com.orangelife.redis.core.redis.session.redisSession.WebServiceSession;
import com.orangelife.session.Session;
import com.orangelife.session.impl.SimpleSessionManager;
import com.orangelife.setting.constant.ContextConst;
import com.orangelife.utils.LoginSessionContext;

public abstract class AbstractV2TransferService extends ResponseSupportSubLogic {

	/* log initation */
	protected Logger logger = LoggerFactory
			.getLogger(AbstractV2TransferService.class);

	@Resource(name = "sessionManager")
	private SimpleSessionManager sessionManager;

	protected WebServiceSession getWebServiceSession(
			HttpServletRequest servletRequest) {

		if (servletRequest == null)
			return null;
		String id = servletRequest.getHeader(ContextConst.ACCESS_TOKEN);
		WebServiceSession w = RedisSessionFactory.getRedisSession(id);

		return w;

	}
	protected WebServiceSession getWebServiceSession(
			String token) {


		WebServiceSession w = RedisSessionFactory.getRedisSession(token);

		return w;

	}

	protected WebServiceSession createWebServiceSession(String id) {

		return RedisSessionFactory.createRedisSession(id);
	}

	protected Session getSession(HttpServletRequest request) {
		if (request == null)
			return null;

		String id = request.getHeader(ContextConst.ACCESS_TOKEN);
		if (id == null) {
			return null;
		}
		return sessionManager.getSession(id);
	}

	protected Session createSession() {
		return sessionManager.create();
	}

	protected void removeSession(String id) {
		sessionManager.remove(id);
	}

	protected Long getLoginuserID(HttpServletRequest request) {
		LoginSessionContext logininfo = (LoginSessionContext) getSession(
				request).getAttribute(Session.LOGIN_SESSION_CONTEXT);
		return logininfo.getuserID();
	}

	protected String getLoginUserName(HttpServletRequest request) {
		LoginSessionContext logininfo = (LoginSessionContext) getSession(
				request).getAttribute(Session.LOGIN_SESSION_CONTEXT);
		return logininfo.getLoginName();
	}
	
	


}
